package movie;

import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.json.JSONException;

import datamodel.MovieInfo;
import datamodel.Movies;

import net.sf.jtmdb.GeneralSettings;
import net.sf.jtmdb.Genre;
import net.sf.jtmdb.Movie;

public class TMDB_getMovieInfo {
	private String MovieName = null;
	private int MovieID, Year;
	private Movies movielist;

	public TMDB_getMovieInfo(String MovieName, int Year, Movies movielist) {
		this.MovieName = MovieName;
		this.Year = Year;
		this.movielist = movielist;
	}

	public void getMovieInfo() {
		GeneralSettings.setApiKey("1d51cef38e888586924becd5f1cc9b1d");
		this.MovieName.trim();

		// create a list of movies with the title MovieName.
		try {
			List<Movie> m = Movie.search(this.MovieName);
			// search through list of movies.
			if(this.MovieName != null){
				for (Movie movie: m){
					// check if title of movie is the same as MovieName.
					if(movie.getName().equalsIgnoreCase(this.MovieName)){
						// get the year of the release date of the movie.
						int releasDate = movie.getReleasedDate().getYear()+1900;
						if(releasDate == Year){
							MovieInfo info = new MovieInfo();
							info.setTitle(MovieName);
							info.setYear(Year);
							Movie MovieFull = Movie.getInfo(movie.getID());
							for (Genre genre : MovieFull.getGenres()){
								info.addGenre(genre.getName());
							}
							movielist.addMovie(MovieName, Year, info);
						}
					}
				}
			}

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}